#include "HeroSp.h"
#include "GameManager.h"
#include "GameCenterScene.h"
HeroSp::HeroSp()
{
}


bool HeroSp::init()
{
	if (!Sprite::init())
	{
		return false;
	}
	bb_JumpEd = false;
	isDaoju1=false;
	isDaoju2=false;
	isDaoju3=false;

	maxSpeed = 22;
	cutSpeed = 0;
	valFristY = 232;
	timeCound=0;
	value_Daoju1=-1;
	value_Daoju2=-1;
	value_Daoju3=-1;

	return true;
}
void HeroSp::toDie(){
	action->play("dead", false);
	if (iBiao == 1){
		action->setLastFrameCallFunc([=](){
			CCLOG("12345612");
			//action->play("run", true);
			GameCenter::getInstance()->gameOver();
		});
	}
}
void HeroSp::jumpUp(){
	if (bb_JumpEd) return;
	if (GameCenter::getInstance()->isGameOver) return;
	action->play("jump", false);
	//action->set
	action->setLastFrameCallFunc([=](){
		CCLOG("12345612");
		//action->play("run", true);
		action->clearLastFrameCallFunc();
		cutSpeed = maxSpeed;
		bb_JumpEd = true;
	});
}
void HeroSp::updateY(float dt){
	timeCound+=dt;
	if(timeCound>=2){
		timeCound=0;
		if (value_Daoju1>=0) value_Daoju1--;
		if (value_Daoju2>=0) value_Daoju2--;
		if (value_Daoju3>=0) value_Daoju3--;
		if (value_Daoju1==0)
		{
			closeEffect(1);
		}
		if (value_Daoju2==0)
		{
			GameCenter::getInstance()->reductionSpeed();
			closeEffect(2);
		}
		if (value_Daoju3==0)
		{
			closeEffect(3);
		}
	}
	if (!bb_JumpEd) return;
	this->setPositionY(this->getPositionY() + cutSpeed);
	cutSpeed--;
	if (this->getPositionY() < (valFristY - 1)){
		bb_JumpEd = false;
		cutSpeed = 0;
		action->play("run", true);
		this->setPositionY(valFristY);
		//action->reverse()
	}
}
void HeroSp::closeEffect(int id){
	switch (id)
	{
	case 1:
		isDaoju1 = false;
		action2->gotoFrameAndPause(0);
		rootNode2->setVisible(false);
		break;
	case 2:
		isDaoju2 = false;
		action3->gotoFrameAndPause(0);
		rootNode3->setVisible(false);
		break;
	case 3:
		isDaoju3 = false;
		action1->gotoFrameAndPause(0);
		rootNode1->setVisible(false);
		break;
	default:
		break;
	}
}
void HeroSp::showEffect(int id){
	switch (id)
	{
	case 1:
		isDaoju1 = true;
		action2->play("start", true);
		rootNode2->setVisible(true);
	break;
	case 2:
		value_Daoju2=5;
		isDaoju2 = true;
		action3->play("start", true);
		rootNode3->setVisible(true);
		break;
	case 3:
		value_Daoju3=5;
		isDaoju3 = true;
		action1->play("start", false);
		rootNode1->setVisible(true);
		break;
	default:
		break;
	}
}
Rect HeroSp::getHeroRec(){
	if (iBiao==1)
	{
		return Rect(thisRect.getMinX()+this->getPositionX(),thisRect.getMinY()+this->getPositionY(),thisRect.size.width/2,thisRect.size.height);
	}else
	{
		return Rect(thisRect.getMinX()+this->getPositionX()+thisRect.size.width/2,thisRect.getMinY()+this->getPositionY(),thisRect.size.width/2,thisRect.size.height);
	}
}
void HeroSp::setHero(int va){
	Node* rootNode;
	num_Briefs = 0;
	Sprite* rotateArrow=Sprite::create("hitPic.png");
	iBiao=va;
	if (va==1)
	{
		rootNode = CSLoader::createNode("spHero.csb");
		addChild(rootNode,2);
		rootNode->setPosition(-22, 15);
		rotateArrow->setPosition(-22+31+30,15-16+10);
		action = CSLoader::createTimeline("spHero.csb");
		rootNode->runAction(action);
	}
	else
	{
		rootNode = CSLoader::createNode("spGril.csb");
		addChild(rootNode,2);
		rotateArrow->setPosition(-13,-10+10);
		action = CSLoader::createTimeline("spGril.csb");
		rootNode->runAction(action);
	}
	action->play("run", true);
	CCLOG("HeroSp=%f,%f",rootNode->boundingBox().size.width,rootNode->boundingBox().size.height);

	
	//rotateArrow->setPosition(-10,-56);
	rotateArrow->setVisible(false);
	addChild(rotateArrow,5);
	thisRect=rotateArrow->boundingBox();

	rootNode1 = CSLoader::createNode("spProps1.csb");
	addChild(rootNode1,3);
	action1 = CSLoader::createTimeline("spProps1.csb");
	rootNode1->runAction(action1);

	rootNode2 = CSLoader::createNode("spProps2.csb");
	rootNode2->setScale(1.5);
	rootNode2->setPosition(-10,-56);
	addChild(rootNode2, 1);
	action2 = CSLoader::createTimeline("spProps2.csb");
	rootNode2->runAction(action2);

	rootNode3 = CSLoader::createNode("spProps3.csb");
	addChild(rootNode3, 4);
	action3 = CSLoader::createTimeline("spProps3.csb");
	rootNode3->runAction(action3);

	rootNode1->setVisible(false);
	rootNode2->setVisible(false);
	rootNode3->setVisible(false);

	//action2->play("start", true);
	//showEffect(3);

}